package si.sp.socialnetwork;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class SearchFriends extends HttpServlet {

	private static final long serialVersionUID = 1L;

	private ArrayList<User> friends = new ArrayList<User>();

	private DatabaseBean db_bean;

	/** 
	 * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
	 * @param request servlet request
	 * @param response servlet response
	 * @throws ServletException if a servlet-specific error occurs
	 * @throws IOException if an I/O error occurs
	 */
	protected void processRequest(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String name = request.getParameter("friend_search");
		friends = db_bean.getMatchingUsers(name); //vrne arraylist ujemajocih osebkov s parametrom name
		//friends.add(new User("Ana","Benedik","Kranj",1));

		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out = response.getWriter();

		for(User f : friends){
			out.println("<div class=\"result\">");
			out.println("<form action=\"utrinkiMojiPrijatelji.jsp\" method=\"post\">");
			out.println("<p>");
			out.println("<span class=\"friend_name\">" + f.getIme()+ "</span> <span class=\"friend_surname\">" + f.getPriimek()+ "</span>, <span class=\"friend_place\">" + f.getKraj()+ "</span>");
			out.println("</p>");
			out.println("<p class=\"friend_t\">");
			out.println("<label>Sodi v krog <select name=\"group\" class=\"selected_group\">");
			out.println("<option value=\"znanci\" selected=\"selected\">Znanci</option>");
			out.println("<option value=\"družina\">Družina</option>");
			out.println("<option value=\"zaupni prijatelji\">Zaupni prijatelji</option> ");
			out.println("</select></label>");
			out.println("</p>");
			out.println("<p class=\"friend_t\">");
			out.println("<label>Klikni na gumb s številko, če hočeš, da postane tvoj prijatelj ");
                        out.println("<input type=\"submit\" class=\"button_style\" name=\"dodaj_prijatelja\" value=\""+f.getIDuser()+"\") />");
			out.println("</label>");
			out.println("</p>");
			out.println("</form>");
			out.println("</div>");            
		}       

	}

	/** 
	 * Handles the HTTP <code>POST</code> method.
	 * @param request servlet request
	 * @param response servlet response
	 * @throws ServletException if a servlet-specific error occurs
	 * @throws IOException if an I/O error occurs
	 */
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		processRequest(request, response);
	}
}